home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsI / Src / 2DClip / clip.c < prev    next >
Text File  |  1992-06-16  |  3KB  |  178 lines

  1. /*
  2.  * file clip.c
  3.  *    contains the actual clipping routines
  4.  */
  5. #include    <stdio.h>
  6. #include    "GraphicsGems.h"
  7. #include    "line.h"
  8.  
  9. /*
  10.  * vis_vector
  11.  *
  12.  *    PURPOSE
  13.  *    actual user interface. Draws a clipped line
  14.  *    NOTE: coordinates are given in converted LONGS!
  15.  *
  16.  *    xf, yf    from coordinates of vector to be drawn
  17.  *    xt, yt    to coordinates of vector to be drawn
  18.  */
  19. vis_vector(xf, yf, xt, yt)
  20. long    xf, yf, xt, yt;
  21. {
  22.     SEGMENT    l;
  23.  
  24.     if(xf == xt && yf == yt)
  25.         return;
  26.     l._from._x = xf;
  27.     l._from._y = yf;
  28.     l._to._x   = xt;
  29.     l._to._y   = yt;
  30. /*
  31.  * start at top of list
  32.  */
  33.     clip(CL, &l);
  34. }
  35.  
  36. /*
  37.  * clip
  38.  *
  39.  *    PURPOSE
  40.  *
  41.  *    p    pointer to polygon
  42.  *    l    pointer to line segment
  43.  */
  44. clip(p, l)
  45. CONTOUR    *p;
  46. SEGMENT    *l;
  47. {
  48.     SEGMENT    *sp, ss;
  49.     CLIST    *sol;
  50.     POINT    pt;
  51.     boolean    up, delay, inside, p_inside(), disjunct();
  52.     int    i;
  53.     short    nsol, nsmax = 2;
  54.  
  55.  
  56. /*
  57.  * list exhausted do what you like
  58.  * we want to plot
  59.  */
  60.     if(p == (CONTOUR *)NULL) {
  61.         move((l->_from._x), (l->_from._y));
  62.         cont((l->_to._x), (l->_to._y));
  63.         return;
  64.     }
  65. /*
  66.  * polygon is switched off
  67.  * take next one
  68.  */
  69.     if(!IS_ON(p)) {
  70.         clip(p->_next, l);
  71.         return;
  72.     }
  73. /*
  74.  * comparison on basis of the
  75.  * enclosing rectangle
  76.  */
  77.     if(disjunct(p, l)) {
  78.         if(!IS_NORMAL(p)) {
  79.             clip(p->_next, l);
  80.         }
  81.         return;
  82.     }
  83. /*
  84.  * calculate possible intersections
  85.  */
  86.     sol = (CLIST *) calloc(2, sizeof(CLIST));
  87.     sol[0]._p._x = l->_from._x;
  88.     sol[0]._p._y = l->_from._y;
  89.     sol[0]._type = STD;
  90.     sol[1]._p._x = l->_to._x;
  91.     sol[1]._p._y = l->_to._y;
  92.     sol[1]._type = STD;
  93.     nsol = 2;
  94.     cross_calc(p, l, &sol, &nsol, nsmax);
  95.     pt._x = sol[0]._p._x;
  96.     pt._y = sol[0]._p._y;
  97.  
  98. /*
  99.  * determine status of first point
  100.  */
  101.     inside = p_inside(p, &pt);
  102.     if((!inside && IS_NORMAL(p)) || (inside && !IS_NORMAL(p)))
  103.         up = TRUE; 
  104.     else
  105.         up = FALSE;
  106.     delay = FALSE;
  107. /*
  108.  * process list of intersections
  109.  */
  110.     for(i=1; i<nsol; i++) {
  111.         if(!up) {
  112.             ss._from._x = sol[i-1]._p._x;
  113.             ss._from._y = sol[i-1]._p._y;
  114.             ss._to._x = sol[i]._p._x;
  115.             ss._to._y = sol[i]._p._y;
  116.             clip(p->_next, &ss);
  117.         }
  118.         if(!delay) {
  119.             if(sol[i]._type != DELAY)
  120.                 up = (up) ? FALSE : TRUE;
  121.             else
  122.                 delay = TRUE;
  123.         }
  124.         else {
  125.             up = (up) ? FALSE : TRUE;
  126.             delay = FALSE;
  127.         }
  128.     }
  129.     free(sol);
  130. }
  131.  
  132. /*
  133.  * disjunct
  134.  *
  135.  *    PURPOSE
  136.  *    determine if the box enclosing the polygon 
  137.  *    stored in p and the box enclosing the line 
  138.  *    segment stored in l are disjunct.
  139.  *    Return TRUE if disjunct else FALSE
  140.  *
  141.  *    p    points to the polygon structure
  142.  *    l    points to the linesegment structure    
  143.  *
  144.  */
  145. boolean    disjunct(p, l)
  146. CONTOUR    *p;
  147. SEGMENT    *l;
  148.  
  149. {
  150.     if((max(l->_from._x, l->_to._x) < p->_minx) ||
  151.        (min(l->_from._x, l->_to._x) > p->_maxx) ||
  152.            (max(l->_from._y, l->_to._y) < p->_miny) ||
  153.        (min(l->_from._y, l->_to._y) > p->_maxy)   )
  154.         return(TRUE);
  155.     else
  156.         return(FALSE);
  157. }
  158.  
  159. #define DEBUG
  160. #ifdef DEBUG
  161. move(x, y)
  162. long    x, y;
  163. {
  164.     printf("(%d,%d) ->", x, y);
  165. }
  166.  
  167. cont(x, y)
  168. long    x, y;
  169. {
  170.     printf("(%d,%d)\n", x, y);
  171. }
  172.  
  173. #endif
  174.  
  175.  
  176.  
  177.  
  178.